package com.yidu.ladder.base.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yidu.ladder.base.dao.RipareaDao;
import com.yidu.ladder.base.domain.Riparea;
import com.yidu.ladder.base.domain.RipareaCustomer;
import com.yidu.ladder.base.domain.RipareaStaff;
import com.yidu.ladder.base.domain.RipareaVo;
import com.yidu.ladder.base.domain.Staff;
import com.yidu.ladder.base.service.RipareaService;
import com.yidu.ladder.system.domain.Customer;
import com.yidu.ladder.util.IDGenerator;
/**
 * 定区业务实现类
 * @author lw
 * @since 2020/1/10
 */
@Service
public class RipareaServiceImpl implements RipareaService{
	
	/**
	 * 保留对定区数据访问层的依赖注入
	 */
	@Autowired
	private RipareaDao ripareaDao;
	
	public RipareaServiceImpl() {
		super();
	}

	public RipareaServiceImpl(RipareaDao ripareaDao) {
		super();
		this.ripareaDao = ripareaDao;
	}
	
	public RipareaDao getRipareaDao() {
		return ripareaDao;
	}

	public void setRipareaDao(RipareaDao ripareaDao) {
		this.ripareaDao = ripareaDao;
	}

	@Override
	public Map<String, Object> findAllRipareaWithPage(RipareaVo ripareaVo) {
		//计算分页
		ripareaVo.setPage((ripareaVo.getPage() - 1) * ripareaVo.getLimit());
		//查询所有
		List<Riparea> ripareaList = ripareaDao.findAllRipareaWithPage(ripareaVo);
		//计总数
		int total = ripareaDao.countRiparea(ripareaVo);
		//定义一个Map对象
		Map<String, Object> map = new HashMap<String, Object>();
		//将数据封装到结果映射中
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", total);
		map.put("data", ripareaList);
		return map;
	}

	@Override
	public boolean addRiparea(Riparea riparea) {
		//给定区Id赋值
		riparea.setRipAreaId(IDGenerator.getId());
		//给职员Id赋值
		riparea.setStaffId(IDGenerator.getId());
		//给状态赋值
		riparea.setUseable("Y");
		//调用添加方法
		int result = ripareaDao.addRiparea(riparea);
		return result == 0 ? false : true;
	}

	@Override
	public boolean updataRiparea(Riparea riparea) {
		int result = ripareaDao.updataRiparea(riparea);
		return result == 1 ? true : false;
	}

	@Override
	public boolean frozenORrecoverRiparea(String ripAreaIdStr, boolean flag) {
		//定义一个map对象
		Map<String, Object> paramMap = new HashMap<String, Object>();
		//判断进行冻结还是还原
		char useable = flag == true ? 'N' : 'Y';
		//根据逗号分割ID
		String[] ripAreaIds = ripAreaIdStr.split(",");
		//定义执行结果返回的总数 因为成功返回1，所以把所有执行结果加起来正好就等于ID的总数
		int total = 0;
		for (int i =0; i < ripAreaIds.length; i++) {
			//添加ID
			paramMap.put("ripAreaId", ripAreaIds[i]);	
			//添加要修改的状态
			paramMap.put("useable", useable);
			//调用逻辑删除或还原方法
			int result = ripareaDao.frozenORrecoverRiparea(paramMap);
			//计数
			total += result;
		}
		return total == ripAreaIds.length ? true : false;
	}

	@Override
	public List<Staff> findAllStaffByRipAreaId(String ripAreaId) {
		List<Staff> result = ripareaDao.findAllStaffByRipAreaId(ripAreaId);
		return result;
	}

	@Override
	public boolean addRipareaStaff(Riparea riparea,Staff staff) {
		//定义一个定区职员对象
		RipareaStaff ripareaStaff = new RipareaStaff();
		//给定区职员Id赋值
		ripareaStaff.setRipAreaStaffId(IDGenerator.getId());
		//给定区Id赋值
		ripareaStaff.setRipAreaId(riparea.getRipAreaId());
		//给定区名赋值
		ripareaStaff.setRipAreaName(riparea.getRipAreaName());
		//给职员Id赋值
		ripareaStaff.setStaffId(staff.getStaffId());
		//给职员名赋值
		ripareaStaff.setStaffName(staff.getStaffName());
		//调用添加方法
		int result = ripareaDao.addRipareaStaff(ripareaStaff);
		return result == 1 ? true : false;
	}

	@Override
	public List<RipareaStaff> findRipareaStaffByRipAreaId(String ripAreaId) {
		List<RipareaStaff> result = ripareaDao.findRipareaStaffByRipAreaId(ripAreaId);
		return result;
	}

	@Override
	public Riparea findRipareaByRipareaId(String ripAreaId) {
		return ripareaDao.findRipareaByRipareaId(ripAreaId);
	}

	@Override
	public Staff findStaffByStaffId(String staffId) {
		return ripareaDao.findStaffByStaffId(staffId);
	}

	@Override
	public boolean deleteRipareaStaff(String ripAreaId, String staffId) {
		int result = ripareaDao.deleteRipareaStaff(ripAreaId, staffId);
		return result == 1 ? true : false;
	}

	@Override
	public List<Customer> findAllCustomer() {
		List<Customer> result = ripareaDao.findAllCustomer();
		return result;
	}

	@Override
	public List<RipareaCustomer> findRipareaCustomerByRipAreaId(String ripAreaId) {
		return ripareaDao.findRipareaCustomerByRipAreaId(ripAreaId);
	}

	@Override
	public Customer findCustomerByCustomerId(String customerId) {
		return ripareaDao.findCustomerByCustomerId(customerId);
	}

	@Override
	public boolean addRipareaCustomer(Riparea riparea, Customer customer) {
		//定义一个定区客户对象
		RipareaCustomer ripareaCustomer = new RipareaCustomer();
		//给定区客户Id赋值
		ripareaCustomer.setRipAreaCustomerId(IDGenerator.getId());
		//给定区Id赋值
		ripareaCustomer.setRipAreaId(riparea.getRipAreaId());
		//给定区名赋值
		ripareaCustomer.setRipAreaName(riparea.getRipAreaName());
		//给客户Id赋值
		ripareaCustomer.setCustomerId(customer.getCustomerId());
		//给客户名赋值
		ripareaCustomer.setCustomerName(customer.getCustomerName());
		//调用添加方法
		int result = ripareaDao.addRipareaCustomer(ripareaCustomer);
		return result == 1 ? true : false;
	}

	@Override
	public boolean deleteRipareaCustomer(String ripAreaId, String customerId) {
		int result = ripareaDao.deleteRipareaCustomer(ripAreaId, customerId);
		return result == 1 ? true : false;
	}

	@Override
	public List<Staff> findAllStaffName() {
		return ripareaDao.findAllStaffName();
	}

	

}
